package cn.yunyichina.provider.framework.dubbo.service;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.iface.entity.cache.SessionInfo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.service.ICache;
import cn.yunyichina.utils.commons.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * 缓存服务的调用
 * 
 * @author zhangkewen
 * 
 */
public class CacheService {

    static final String serviceName = "cacheService";

    static ICache cacheService = null;

    static {
        cacheService = (ICache) SpringContextHandler.getBean(serviceName);
    }
    
    public static ICache getCacheClient(){
    	return cacheService;
    }

    /**
     * 设置全局Key
     * 
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital
     * @param key
     *            key
     * @param jsonData
     *            json数据
     * @param expireTime
     *            超时时间 单位:秒 0为不超时，其他设置超时时间
     * @return
     */
    public static Response setGlobalKey(String system, String region, String key, String jsonData, int expireTime) {
        return cacheService.setGlobalKey(system, region, key, jsonData, expireTime);
    }

    /**
     * 设置会话key，默认超时时间：30分钟
     * 
     * @param sessionId
     *            会话sessionid
     * @param key
     *            键值，按"."分隔
     * @param jsonData
     *            json数据
     * @return
     */
    public static boolean setSessionKey(String sessionId, String key, String jsonData) {
        return cacheService.setSessionKey(sessionId, key, jsonData);
    }
    
    public static boolean setSessionInfo(String sessionId, SessionInfo sessionInfo) {
        return cacheService.setSessionKey(sessionId, "sessioninfo", JSON.toJSONString(sessionInfo));
    }
    
    public static boolean setSessionInfo(String sessionId,int deviceType, SessionInfo sessionInfo) {
        return cacheService.setSessionKey(sessionId, "sessioninfo", deviceType,JSON.toJSONString(sessionInfo));
    }

    /**
     * 获取全局Key
     * 
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital
     * @param key
     *            键值
     * @return
     */
    public static String getGlobalKey(String system, String region, String key) {
        return cacheService.getGlobalKey(system, region, key);
    }

    /**
     * 获取会话Key的内容
     * 
     * @param sessionId
     * @param key
     * @return
     */
    public static String getSessionKey(String sessionId, String key) {
        return cacheService.getSessionKey(sessionId, key);
    }
    
    public static SessionInfo getSessionInfo(String sessionId) {
        String jsonData=cacheService.getSessionKey(sessionId, "sessioninfo");
        if(!StringUtils.isBlank(jsonData)){
            //重新刷新session的超时时间
            cacheService.setSessionKeyExpireTime(sessionId, "sessioninfo");
            return JSON.parseObject(jsonData,SessionInfo.class);
        }
        return null;
    }
    
    

    /**
     * 删除全局Key
     * 
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital
     * @param key
     *            键值
     * @return
     */
    public static boolean delGlobalKey(String system, String region, String key) {
        return cacheService.delGlobalKey(system, region, key);
    }

    /**
     * 
     * @param sessionId
     * @param key
     * @return
     */
    public static boolean delSessionKey(String sessionId, String key) {
        return cacheService.delSessionKey(sessionId, key);
    }

    public static Response callUnderlineFromInternal(String method, String data) {
        return cacheService.callUnderlineFromInternal("", method, data, (short) 0);
    }

    public static Response callUnderlineFromInternal(String sessionId, String method, String data) {
        return cacheService.callUnderlineFromInternal(sessionId, method, data, (short) 0);
    }

    public static Response callHumpFromInternal(String sessionId, String method, String data) {
        return cacheService.callHumpFromInternal(sessionId, method, data, (short) 0);
    }

    public static Response callHumpFromInternal(String method, String data) {
        return cacheService.callHumpFromInternal("", method, data, (short) 0);
    }

    /**
     * 全局产生订单号
     * 
     * @param bizType
     *            业务类型 1-挂号 2-门诊 3-住院 4-押金 5-支付流水号
     * @param tradeType
     *            1-支付 2-退款
     * @return
     */
    public static String generateOrderNum(int bizType, int tradeType) {
        return cacheService.generateOrderNum(bizType, tradeType);
    }

    /**
     * 获取过期时间，单位：秒
     * 
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital
     * @param key
     *            键值
     * @return 过期时间，秒
     */
    public static long ttl(String system, String region, String key) {
    	return cacheService.ttl(system, region, key);
    }


    /**
     * 设置全局Key的hash中的field对应的value值
     *
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital.branch
     * @param key
     *            key szszyy.254156X
     * @param field
     *
     * @param jsonData
     *            json数据
     * @return
     */
    public static Response setGlobalKeyHash(String system, String region, String key, String field,String jsonData){
        return cacheService.setGlobalKeyHash(system, region, key,field,jsonData);
    }

    /**
     * 获取全局Key的hash中的field对应的value值
     *
     * @param system
     *            系统名 如:base,hisface,cache,authorize等
     * @param region
     *            区域 如:hospital.branch
     * @param key
     *            key szszyy.254156X
     * @param field
     * @return
     */
    public static String getGlobalKeyHash(String system, String region, String key,String field){
        return cacheService.getGlobalKeyHash(system, region, key,field);
    }

    public static boolean delGlobalKeyHash(String system, String region, String key,String field) {
        return cacheService.delGlobalKeyHash(system, region, key,field);
    }
}
